<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit">
    <a-form :form="form">
      <a-form-item style="display:none"><a-input v-decorator="['classesId']" /></a-form-item>
      <a-form-item style="display: none;"><a-input placeholder="系别名称" v-decorator="['deptName']" /></a-form-item>
      <a-form-item style="display: none;"><a-input placeholder="专业名称" v-decorator="['specialityName']" /></a-form-item>
      <a-form-item style="display: none;"><a-input v-decorator="['directorName']" /></a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="年级">
        <a-input type="number" placeholder="年级(例:2020)" v-decorator="['grade', { rules: [{ required: true, message: '请输入年级' }] }]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="教育层次">
        <a-select placeholder="教育层次" v-decorator="['educationalLevel', { rules: [{ required: true, message: '请选择教育层次' }] }]" allowClear>
          <a-select-option v-for="(b, index) in educationalLevelList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="班级名称">
        <a-input placeholder="班级名称" v-decorator="['classesName', { rules: [{ required: true, message: '请输入班级名称' }] }]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="系别">
        <a-tree-select
          v-decorator="['deptId', { rules: [{ required: true, message: '请选择系别' }] }]"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="deptTree"
          @change="onChange"
          placeholder="请选择系别"
          treeDefaultExpandAll
        ></a-tree-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="专业">
        <a-select placeholder="请选择专业" v-decorator="['specialityCode', { rules: [{ required: true, message: '请选择专业' }] }]">
          <a-select-option
            @click="form.setFieldsValue({ specialityName: item.specialityName })"
            v-for="item in specialityList"
            :key="item.specialityCode"
            :value="item.specialityCode"
          >
            {{ item.specialityName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="班主任">
        <a-select
          :options="teacherList"
          showSearch
          optionFilterProp="children"
          v-decorator="['directorId']"
          placeholder="请选择班主任">
        </a-select>
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
        <a-radio-group name="classesStatus" v-decorator="['classesStatus']">
          <a-radio v-for="item in classesStatusList" :key="item.dictValue" :value="item.dictValue">{{ item.dictLabel }}</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveSysClasses } from '@/api/classes'
import { getTeacherList } from '@/api/teacher'
import { getSpecialityList } from '@/api/speciality'
import pick from 'lodash.pick'
export default {
  name: 'ClassesModal',
  props: {
    deptTree: {
      type: Array,
      required: true
    },
    classesStatusList: {
      type: Array,
      required: true
    },
    educationalLevelList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      specialityList: [],
      teacherList: [],
      visible: false,
      labelCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 5
        }
      },
      wrapperCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 16
        }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {},
  created () {},
  methods: {
    add () {
      this.form.resetFields()
      this.edit({
        classesId: 0,
        classesStatus: '1'
      })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        getSpecialityList({
          deptId: this.mdl.deptId
        }).then(res => {
          this.specialityList = res
        })
        getTeacherList({
          // deptId: this.mdl.deptId
        }).then(res => {
          this.teacherList = res.map(item => {
            item.value = item.teacherId
            item.label = `${item.name}(${item.jobNumber})-${item.deptName}`
            item.key = item.jobNumber
            return item
          })
        })
        this.form.setFieldsValue(
          pick(this.mdl, 'classesId', 'classesName', 'directorId', 'directorName', 'deptId', 'deptName', 'specialityCode', 'specialityName', 'grade', 'classesStatus', 'educationalLevel')
        )
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveSysClasses(values)
            .then(res => {
              if (res.code === 0) {
                this.$message.success('保存成功')
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    onChange (value, label, extra) {
      if (extra.triggerNode.$children.length > 0) {
        this.$message.error('不能选择父节点' + `${label}`)
        this.deptCheck = false
      } else {
        this.deptCheck = true
        this.$nextTick(() => {
          this.form.setFieldsValue({
            deptName: label[0],
            specialityName: undefined,
            specialityCode: undefined
          })
        })
        getSpecialityList({
          deptId: value
        }).then(res => {
          this.specialityList = res
        })
      }
    }
  },
  watch: {
    /*
        'selectedRows': function (selectedRows) {
          this.needTotalList = this.needTotalList.map(item => {
            return {
              ...item,
              total: selectedRows.reduce( (sum, val) => {
                return sum + val[item.dataIndex]
              }, 0)
            }
          })
        }
        */
  }
}
</script>
